<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/3/16 14:57
 * Version: 1.0
 */

namespace app\admin\controller;

use think\Db;

class Member extends Base
{
    public function index()
    {

        $p = input('?p') ? intval(input('p')) : '1';
        $field = input('?field') ? input('field') : '';
        $keyword = input('?keyword') ? htmlentities(input('keyword')) : '';
        $order = input('?order') ? input('order') : 'DESC';
        $where = '';

        //查询条件
        $prefix = config('database.prefix');
        if ($order == 'asc') {
            $order = "t asc";
        } elseif (($order == 'desc')) {
            $order = "t desc";
        } else {
            $order = "uid asc";
        }
        if ($keyword <> '') {
            if ($field == 'user') {
                $where = "{$prefix}user.user LIKE '%$keyword%'";
            }
            if ($field == 'phone') {
                $where = "{$prefix}user.phone LIKE '%$keyword%'";
            }
            if ($field == 'qq') {
                $where = "{$prefix}user.qq LIKE '%$keyword%'";
            }
            if ($field == 'email') {
                $where = "{$prefix}user.email LIKE '%$keyword%'";
            }
        }

        $user = db('user');
        //分页
        $pagesize = 10;#每页数量

        $list = $user->alias('u')
            ->order('u.'.$order)
            ->join("{$prefix}auth_group_access a","u.uid = a.uid")
            ->join("{$prefix}auth_group g","g.id = a.group_id")
            ->where($where)
            ->field("{$prefix}user.*,g.id as gid,g.title")
            ->paginate($pagesize);

        $this->assign('list', $list);
        $group = db('auth_group')->field('id,title')->select();
        $this->assign('group', $group);
        return view();
    }

    public function del()
    {
        $uids = input('uids');
        $uids = isset($uids) ? $uids : false;
        //uid为1的禁止删除
        if ($uids == 1 or !$uids) {
            return json(['flag'=>1,'message'=>'参数错误!']);

        }
        $del = model('Member')->member_del($uids);
        if ($del) {
            return json(['flag'=>0,'message'=>'删除成功!']);
        } else {
            return json(['flag'=>1,'message'=>'删除失败!']);
        }
    }


    public function add()
    {
        $usergroup = model('member')->user_group();
        $this->assign('usergroup', $usergroup);
        return view('form');
    }

    public function edit()
    {
        $uid = input('uid');
        $uid = isset($uid) ? intval($uid) : false;
        if ($uid) {
            //$member = M('user')->where("uid='$uid'")->find();
            $prefix = config('DB_PREFIX');
            $user = db('user');
            $user = $user->alias('u')
                ->join("{$prefix}auth_group_access a"," u.uid = a.uid")
                ->field("u.*,a.group_id")
                ->where("u.uid=$uid")
                ->find();
        } else {
            $this->error('参数错误！');
        }

        $usergroup = model('member')->user_group();
        $this->assign('usergroup', $usergroup);

        $this->assign('member', $user);
        return view('form');
    }

    public function update($ajax = '')
    {
        //修改权限组
        if ($ajax == 'yes') {
            $uid = input('get.uid', 0, 'intval');
            $gid = input('get.gid', 0, 'intval');
            db('auth_group_access')->data(array('group_id' => $gid))->where("uid='$uid'")->update();
            die('1');
        }

        //
        $info = input('post.');

        $uid = isset($info['uid']) ? $info['uid'] : false;
        $user = isset($info['user']) ? $info['user'] : '';
        $group_id = isset($info['group_id']) ? $info['group_id'] : 0;
        if (!$group_id) {
            return json(['flag'=>1,'message'=>'请选择用户组!']);
        }
        $password = isset($info['password']) ? $info['password'] : false;
        if ($password) {
            $data['password'] = password($password);
        }
        $head = input('post.head', '', 'strip_tags');
        $data['sex'] = isset($info['sex']) ? intval($info['sex']) : 0;
        $data['head'] = $head ? $head : '';
        $data['birthday'] = isset($info['birthday']) ? strtotime($info['birthday']) : 0;
        $data['phone'] = isset($info['phone']) ? trim($info['phone']) : '';
        $data['qq'] = isset($info['qq']) ? trim($info['qq']) : '';
        $data['email'] = isset($info['email']) ? trim($info['email']) : '';

        $data['t'] = time();

        if (!$uid) {
            if ($user == '') {
                return json(['flag'=>1,'message'=>'用户名称不能为空!']);
            }
            if (!$password) {
                return json(['flag'=>1,'message'=>'用户密码不能为空!']);
            }
            if (db('user')->where("user='$user'")->find()) {
                return json(['flag'=>1,'message'=>'用户名已被占用!']);
            }

            $data['user'] = $user;
            $uid = db('user')->insertGetId($data);
            db('auth_group_access')->data(array('group_id' => $group_id, 'uid' => $uid))->insert();
            addlog('新增会员，会员UID：' . $uid);
            return json(['flag'=>0,'message'=>'添加会员成功!']);
        } else {
            db('auth_group_access')->data(array('group_id' => $group_id))->where("uid=$uid")->update();
            addlog('编辑会员信息，会员UID：' . $uid);
            db('user')->data($data)->where("uid=$uid")->update();
            return json(['flag'=>0,'message'=>'编辑会员信息成功!']);
        }
    }

}